package com.erp.common.utils;

import java.util.Calendar;
import java.util.Date;
import java.util.StringTokenizer;

public class StringUtils extends org.apache.commons.lang3.StringUtils {

    public static final String DEFAULT_PATH_SEPARATOR = ",";
    /**
     * 将字符串对象按给定的分隔符separator转象为字符型数组对象
     * @param str       待转换的符串对象
     * @param separator 字符型分隔符
     * @return 字符型数组
     */
    public static String[] strToStrArray(String str, String separator) {
        return strToStrArrayManager(str, separator);
    }

    /**
     * 将一个中间带逗号分隔符的字符串转换为字符型数组对象
     *
     * @param str
     *            待转换的符串对象
     * @return 字符型数组
     */
    public static String[] strToStrArray(String str) {
        return strToStrArrayManager(str, DEFAULT_PATH_SEPARATOR);
    }

    private static String[] strToStrArrayManager(String str, String separator) {

        StringTokenizer strTokens = new StringTokenizer(str, separator);
        String[] strArray = new String[strTokens.countTokens()];
        int i = 0;

        while (strTokens.hasMoreTokens()) {
            strArray[i] = strTokens.nextToken().trim();
            i++;
        }

        return strArray;
    }

    public static String getCtime() {
        Calendar dt = Calendar.getInstance();
        dt.setTime(new Date());
        String tmp = String.valueOf(dt.get(Calendar.YEAR));
        int m = dt.get(Calendar.MONTH) + 1;
        String mm = "";
        if (m < 10)
            mm += "0";
        mm += String.valueOf(m);

        int d = dt.get(Calendar.DAY_OF_MONTH);
        String dd = "";
        if (d < 10)
            dd += "0";
        dd += String.valueOf(d);

        int h = dt.get(Calendar.HOUR_OF_DAY);
        String hh = "";
        if (h < 10)
            hh += "0";
        hh += String.valueOf(h);

        int min = dt.get(Calendar.MINUTE);
        String minute = "";
        if (min < 10)
            minute += "0";
        minute += String.valueOf(min);

        int s = dt.get(Calendar.SECOND);
        String ss = "";
        if (s < 10)
            ss += "0";
        ss += String.valueOf(s);
        return tmp + mm + dd + hh + minute + ss;
    }

}
